package com.example.shopping_li.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
/*
1.共享参数存储
2.文件存储
3.数据库存储
 */
import com.example.shopping_li.me.UserBean;

import java.util.Map;

public class SaveUserUtils {
    private static UserBean.DataBean userinfo;

    public static UserBean.DataBean getUserinfo() {
        return userinfo;
    }

    public static void setUserinfo(UserBean.DataBean info){
        if (info != null) {
            SaveUserUtils.userinfo = info;
        }
    }
    //删除内存信息
    public  static void delUserInfo(){
        userinfo=null;
    }

    public  static UserBean.DataBean getUserToObj(Context context){
        SharedPreferences preferences=context.getSharedPreferences("user",Context.MODE_PRIVATE);
        int id=preferences.getInt("id",-1);
        if (id != -1) {
            String phone = preferences.getString("phone", "");
            String password = preferences.getString("password", "");
            String name = preferences.getString("name", "");
            String address = preferences.getString("address", "");
            String nickname = preferences.getString("nickname", "");
            String question = preferences.getString("question", "");
            String answer = preferences.getString("answer", "");
            UserBean.DataBean bean = new UserBean.DataBean(id, phone, password, name, address, nickname, question, answer);
            setUserinfo(bean);
            return bean;
        }
        return null;
    }
    public static void saveUserInfo(Context context,UserBean.DataBean bean){
        SharedPreferences preferences=context.getSharedPreferences("user",Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = preferences.edit();
        edit.putInt("id",bean.getId());
        edit.putString("phone",bean.getPhone());
        edit.putString("password",bean.getPassword());
        edit.putString("name",bean.getName());
        edit.putString("address",bean.getAddress());
        edit.putString("nickname",bean.getNickname());
        edit.putString("question",bean.getQuestion());
        edit.putString("answer",bean.getAnswer());
        edit.commit();
        setUserinfo(bean);
    }
    /*
    删除个人信息
     */
public static void deleteUserInfo(Context context,UserBean.DataBean bean){
    SharedPreferences preferences=context.getSharedPreferences("user",Context.MODE_PRIVATE);
    SharedPreferences.Editor edit = preferences.edit();
    edit.remove("id");
    edit.remove("phone");
    edit.remove("password");
    edit.remove("name");
    edit.remove("address");
    edit.remove("nickname");
    edit.remove("question");
    edit.remove("answer");
    edit.commit();
}
}
